﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MsCreation
{

     class DataHandler
    {
         string answer;
         bool isValid;

        public DataHandler()
        {

        }

        public void Save(Hero hero)
        {
            // Check if there is a directory for that hero already
            if (Directory.Exists(@"c:\MsCreation\Save" + hero.Identifier) == false)
            {
                // If there isn't, create one
                Directory.CreateDirectory(@"c:\MsCreation\Save" + hero.Identifier);

                // Add this character to the character list
                StreamWriter charList = new StreamWriter(@"c:\MsCreation\PL.txt", true);
                charList.WriteLine(hero.Identifier);
                charList.Close();
            }

            
            // Open up/create the actual file
            StreamWriter file = new StreamWriter(@"c:\MsCreation\Save" + hero.Identifier + @"\PlayerData.txt");

            // Write to the file
            file.WriteLine(hero.CurrentHealth);
            file.WriteLine(hero.MaxHealth);
            file.WriteLine(hero.CurrentMagic);
            file.WriteLine(hero.MaxMagic);
            file.WriteLine(hero.Strength);
            file.WriteLine(hero.Defense);
            file.WriteLine(hero.Agility);
            file.WriteLine(hero.Experience);
            file.WriteLine(hero.Gold);
            file.WriteLine(hero.AttackDamage);
            file.WriteLine(hero.Identifier);

            // Loop through the player's inventory
            foreach (string item in hero.inventory)
            {
                file.WriteLine(item);
            }

            file.Close();
            Console.WriteLine("Game Save successful");
            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();
        }



        public void Load(Hero hero)
        {
            bool done = false;
            bool listDone = false;
            string item;
            string listItem;


            // Read the characters in the PL, print them out
            StreamReader charList = new StreamReader(@"c:\MsCreation\PL.txt");

            Console.WriteLine();
            Console.WriteLine("Available characters:");
            Console.WriteLine("______________________");

            while (listDone == false)
            {
                listItem = charList.ReadLine();

                if (listItem != null)
                {
                    Console.WriteLine(listItem);
                }
                else
                {
                    listDone = true;
                    Console.WriteLine();
                }
            }

            do
            {
   
                Console.WriteLine("Please enter your hero's name in order to load");
                answer = Console.ReadLine();

                // If the directory doesn't exist, that means there is nothing to load or invalid name
                if (Directory.Exists(@"c:\MsCreation\Save" + answer) == false)
                {
                    Console.WriteLine("That hero doesn't exist yet!");
                    Console.WriteLine();
                    // redundant but whatever
                    isValid = false;
                }
                else
                {
                    isValid = true;
                }
            }
            while (isValid == false);
            
            // Load the actual data file
            StreamReader file = new StreamReader(@"c:\MsCreation\Save" + answer + @"\PlayerData.txt");

            // Read in the file
            hero.CurrentHealth = int.Parse(file.ReadLine());
            hero.MaxHealth = int.Parse(file.ReadLine());
            hero.CurrentMagic = int.Parse(file.ReadLine());
            hero.MaxMagic = int.Parse(file.ReadLine());
            hero.Strength = int.Parse(file.ReadLine());
            hero.Defense = int.Parse(file.ReadLine());
            hero.Agility = int.Parse(file.ReadLine());
            hero.Experience = int.Parse(file.ReadLine());
            hero.Gold = int.Parse(file.ReadLine());
            hero.AttackDamage = int.Parse(file.ReadLine());
            hero.Identifier = file.ReadLine();

            // Continue through the end in order to get all the inventory items
            while (done == false)
            {
                item = file.ReadLine();

                if (item != null)
                {
                    hero.inventory.Add(item);
                }
                else
                {
                    done = true;
                }
            }
            file.Close();
            Console.WriteLine("Load Successful {0}.", hero.Identifier);
            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();
        }
    }
}

